package com.yonyou.brigade.order_center.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yonyou.brigade.basic_init.enums.ChannelEnum;
import com.yonyou.brigade.order_center.dao.BriFlightOrderMapper;
import com.yonyou.brigade.order_center.model.*;
import com.yonyou.brigade.order_center.service.*;
import com.yonyou.ybzboot.starter.common.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.json.JsonObject;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static com.yonyou.brigade.util.resultutil.HttpSendUtils.sendPost;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jiangzhf@yonyou.com
 * @since 2018-06-14
 */
@Service
public class BriFlightOrderServiceImpl extends ServiceImpl<BriFlightOrderMapper, BriFlightOrder> implements IBriFlightOrderService {

    private Logger logger = LoggerFactory.getLogger(BriFlightOrderServiceImpl.class);

    @Autowired
    @Qualifier("briFlightInfoServiceImpl")
    private IBriFlightInfoService briFlightInfoService;
    @Autowired
    @Qualifier("briFlightPassengerServiceImpl")
    private IBriFlightPassengerService briFlightPassengerService;

    @Autowired
    private BriFlightOrderMapper briFlightOrderMapper;

    @Autowired
    private IBriFlightOrderContactService flightContactService;

    @Autowired
    private IInvoiceRelationshipService relationshipService;

    @Override
    public List<BriFlightOrder> getBriFlightOrder(Map params) {
        return briFlightOrderMapper.getBriFlightOrderList(params);
    }

    @Override
    public List<BriFlightOrderDO> getBriFlightOrderDOs(Map params) {
        return briFlightOrderMapper.getBriFlightOrderDOs(params);
    }

    @Value("${ybz.baseUrl}")
    private String ybzBaseUrl;

    @Override
    public List<CnvFlightOrderDO> getCnvFlightOrderDos(Map<String, String> params, Date beginDate, Date endDate) {
        List<CnvFlightOrderDO> cnvFlightOrderDOList = new ArrayList<>();
        // 根据条件查询所有的订单
        Wrapper<BriFlightOrder> flightOrderWrapper = new EntityWrapper<>();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            flightOrderWrapper.eq(entry.getKey(), entry.getValue());
        }
        flightOrderWrapper.ne(true,"is_note",1);
        flightOrderWrapper.ne(true, "order_no", "");
        flightOrderWrapper.ne(true, "category", "");
        List<BriFlightOrder> briFlightOrderList = briFlightOrderMapper.selectList(flightOrderWrapper);

        if (briFlightOrderList != null && briFlightOrderList.size() > 0) {
            for (BriFlightOrder briFlightOrder : briFlightOrderList) {
                CnvFlightOrderDO cnvFlightOrderDO = new CnvFlightOrderDO();
                // 查询订单航班信息
                Wrapper<BriFlightInfo> flightInfoWrapper = new EntityWrapper<>();
                flightInfoWrapper.eq("order_no", briFlightOrder.getOrderNo());
                List<BriFlightInfo> briFlightInfoList = briFlightInfoService.selectList(flightInfoWrapper);
                if (briFlightInfoList.size() > 2) {
                    logger.info("生成记事不支持联程的订单，即往返订单行程数最多为2");
                    continue;
                }
                if (briFlightInfoList.size() > 1) {
                    BriFlightInfo briFlightInfoFirst;
                    BriFlightInfo briFlightInfoSecond;
                    //根据日期前后决定往返程
                    if ((briFlightInfoList.get(1).getDepartureDate() != null && briFlightInfoList.get(1).getDepartureTime() != null)
                            && (briFlightInfoList.get(1).getDepartureDate().after(briFlightInfoList.get(0).getDepartureDate()))
                            || (briFlightInfoList.get(1).getDepartureDate().equals(briFlightInfoList.get(0).getDepartureDate())
                            && briFlightInfoList.get(1).getDepartureTime().after(briFlightInfoList.get(0).getDepartureTime()))) {


                        briFlightInfoFirst = briFlightInfoList.get(0);
                        briFlightInfoSecond = briFlightInfoList.get(1);
                    } else {
                        briFlightInfoFirst = briFlightInfoList.get(1);
                        briFlightInfoSecond = briFlightInfoList.get(0);
                    }
                    //过滤未完成的订单
//                    if (briFlightInfoSecond.getArriveDate() != null && (briFlightInfoSecond.getArriveDate().after(endDate) || briFlightInfoFirst.getArriveDate().before(beginDate))) {
                    if (briFlightInfoSecond.getArriveDate() != null && briFlightInfoSecond.getArriveDate().after(endDate)) {
                        continue;
                    }

                    cnvFlightOrderDO.setBoardPointName(briFlightInfoFirst.getBoardPointName());
                    cnvFlightOrderDO.setBoardPointAirPortName(briFlightInfoFirst.getBoardPointAirPortName());
                    cnvFlightOrderDO.setDepartureDate(briFlightInfoFirst.getDepartureDate());
                    cnvFlightOrderDO.setDepartureTime(briFlightInfoFirst.getDepartureTime());
                    cnvFlightOrderDO.setOffPointName(briFlightInfoFirst.getOffPointName());
                    cnvFlightOrderDO.setOffPointAirPortName(briFlightInfoFirst.getOffPointAirPortName());
                    cnvFlightOrderDO.setArriveDate(briFlightInfoFirst.getArriveDate());
                    cnvFlightOrderDO.setArriveTime(briFlightInfoFirst.getArriveTime());
                    cnvFlightOrderDO.setFlightNo(briFlightInfoFirst.getFlightNo());
                    cnvFlightOrderDO.setYprice(briFlightInfoFirst.getYprice());
                    cnvFlightOrderDO.setClassRebate(briFlightInfoFirst.getClassRebate());
                    cnvFlightOrderDO.setClassLevelName(briFlightInfoFirst.getClassLevelName());
                    cnvFlightOrderDO.setClassLevelCode(briFlightInfoFirst.getClassLevelCode());
                    cnvFlightOrderDO.setLowestPrice(briFlightInfoFirst.getLowestPrice());
                    cnvFlightOrderDO.setOilfee(briFlightInfoFirst.getOilfee());
                    cnvFlightOrderDO.setConstructionPrice(briFlightInfoFirst.getConstructionPrice());

                    cnvFlightOrderDO.setBoardPointName2(briFlightInfoSecond.getBoardPointName());
                    cnvFlightOrderDO.setBoardPointAirPortName2(briFlightInfoSecond.getBoardPointAirPortName());
                    cnvFlightOrderDO.setDepartureDate2(briFlightInfoSecond.getDepartureDate());
                    cnvFlightOrderDO.setDepartureTime2(briFlightInfoSecond.getDepartureTime());
                    cnvFlightOrderDO.setOffPointName2(briFlightInfoSecond.getOffPointName());
                    cnvFlightOrderDO.setOffPointAirPortName2(briFlightInfoSecond.getOffPointAirPortName());
                    cnvFlightOrderDO.setArriveDate2(briFlightInfoSecond.getArriveDate());
                    cnvFlightOrderDO.setArriveTime2((briFlightInfoSecond.getArriveTime()));
                    cnvFlightOrderDO.setFlightNo2(briFlightInfoSecond.getFlightNo());
                    cnvFlightOrderDO.setYprice2(briFlightInfoSecond.getYprice());
                    cnvFlightOrderDO.setClassRebate2(briFlightInfoSecond.getClassRebate());
                    cnvFlightOrderDO.setClassLevelName2(briFlightInfoSecond.getClassLevelName());
                    cnvFlightOrderDO.setClassLevelCode2(briFlightInfoSecond.getClassLevelCode());
                    cnvFlightOrderDO.setLowestPrice2(briFlightInfoSecond.getLowestPrice());
                    cnvFlightOrderDO.setOilfee2(briFlightInfoSecond.getOilfee());
                    cnvFlightOrderDO.setConstructionPrice2(briFlightInfoSecond.getConstructionPrice());
                } else {
                    BriFlightInfo briFlightInfoFirst = null;
                    if (briFlightInfoFirst == null && briFlightInfoList.size() == 0){
                        continue;
                    }
                    if (briFlightInfoList.size() > 1) {
                        logger.info("生成记事不支持联程的订单，即单程订单行程数最多为1");
                        continue;
                    }
                    briFlightInfoFirst = briFlightInfoList.get(0);
                    //过滤未完成的订单
                    if (briFlightInfoFirst.getArriveDate() != null && (briFlightInfoFirst.getArriveDate().after(endDate) || briFlightInfoFirst.getArriveDate().before(beginDate))) {
                        continue;
                    }
                    cnvFlightOrderDO.setBoardPointName(briFlightInfoFirst.getBoardPointName());
                    cnvFlightOrderDO.setBoardPointAirPortName(briFlightInfoFirst.getBoardPointAirPortName());
                    cnvFlightOrderDO.setDepartureDate(briFlightInfoFirst.getDepartureDate());
                    cnvFlightOrderDO.setDepartureTime(briFlightInfoFirst.getDepartureTime());
                    cnvFlightOrderDO.setOffPointName(briFlightInfoFirst.getOffPointName());
                    cnvFlightOrderDO.setOffPointAirPortName(briFlightInfoFirst.getOffPointAirPortName());
                    cnvFlightOrderDO.setArriveDate(briFlightInfoFirst.getArriveDate());
                    cnvFlightOrderDO.setArriveTime(briFlightInfoFirst.getArriveTime());
                    cnvFlightOrderDO.setFlightNo(briFlightInfoFirst.getFlightNo());
                    cnvFlightOrderDO.setYprice(briFlightInfoFirst.getYprice());
                    cnvFlightOrderDO.setClassRebate(briFlightInfoFirst.getClassRebate());
                    cnvFlightOrderDO.setClassLevelName(briFlightInfoFirst.getClassLevelName());
                    cnvFlightOrderDO.setClassLevelCode(briFlightInfoFirst.getClassLevelCode());
                    cnvFlightOrderDO.setLowestPrice(briFlightInfoFirst.getLowestPrice());
                }

                // 机票订单（bri_flight_order）信息
                cnvFlightOrderDO.setOrderNo(briFlightOrder.getOrderNo());
                cnvFlightOrderDO.setOrderType(briFlightOrder.getOrderType());
                cnvFlightOrderDO.setPublicOrPrivate(briFlightOrder.getPublicOrPrivate());
                cnvFlightOrderDO.setPreiumReason(briFlightOrder.getPreiumReason());
                cnvFlightOrderDO.setPaymentPrice(briFlightOrder.getPaymentPrice());
                cnvFlightOrderDO.setApplyOrderNo(briFlightOrder.getApplyOrderNo());
                cnvFlightOrderDO.setFarePrice(briFlightOrder.getFarePrice());
                cnvFlightOrderDO.setPsgCount(briFlightOrder.getPsgCount());
                cnvFlightOrderDO.setStatusType(briFlightOrder.getStatusType());
                cnvFlightOrderDO.setRefundPrice(briFlightOrder.getRefundPrice());
                cnvFlightOrderDO.setTpPrice(briFlightOrder.getTpPrice());
                cnvFlightOrderDO.setTripType(briFlightOrder.getTripType());
                cnvFlightOrderDO.setRelatedNo(briFlightOrder.getRelatedNo());
                cnvFlightOrderDO.setRelatedType(briFlightOrder.getRelatedType());
                cnvFlightOrderDO.setCreateBy(briFlightOrder.getCreateBy());
                cnvFlightOrderDO.setOrderTime(briFlightOrder.getOrderTime());
                cnvFlightOrderDO.setFromType(briFlightOrder.getFromType());
                cnvFlightOrderDO.setNoOrderLowstReason(briFlightOrder.getNoOrderLowstReason());
                cnvFlightOrderDO.setNote(briFlightOrder.getNote());
                cnvFlightOrderDO.setTenant(briFlightOrder.getTenant());
                cnvFlightOrderDO.setUserId(briFlightOrder.getUserId());
                cnvFlightOrderDO.setPayType(briFlightOrder.getPayType());
                cnvFlightOrderDO.setPaymentType(briFlightOrder.getPaymentType());
                cnvFlightOrderDO.setCategory(briFlightOrder.getCategory());
                cnvFlightOrderDO.setRelateYbzId(briFlightOrder.getRelateYbzId());
                cnvFlightOrderDO.setIsDomc(briFlightOrder.getIsDomc());
                cnvFlightOrderDO.setSumInsMoney(briFlightOrder.getSumInsMoney());
                //查询出行人
                Wrapper<BriFlightPassenger> flightPassengerWrapper = new EntityWrapper<>();
                flightPassengerWrapper.eq("order_no", briFlightOrder.getOrderNo());
                List<BriFlightPassenger> flightPassengerList = briFlightPassengerService.selectList(flightPassengerWrapper);
                convertFromBriFlightPassengers(cnvFlightOrderDO, flightPassengerList);
                cnvFlightOrderDO.setId(briFlightOrder.getId());
                cnvFlightOrderDOList.add(cnvFlightOrderDO);
            }
        }
        return cnvFlightOrderDOList;
    }

    public  void updateIsNoteByOrderId(int orderId)
    {
        Map params=new HashMap();
        params.put("id",orderId);
        briFlightOrderMapper.updateIsNoteByOrderId(params);
    }

    public void updateIsBxStateByOrderIds(List<Integer> ids,int bxState)
    {
        Map params=new HashMap();
        params.put("ids",ids);
        params.put("bxState",bxState);
        briFlightOrderMapper.updateIsBxStateByOrderIds(params);
    }





    /*
    该接口只是为服务出现问题，修复时使用
     */
    public List<CnvFlightOrderDO> getCnvFlightOrderDosWithIds(Map<String, String> params, String ids) {
        List<CnvFlightOrderDO> cnvFlightOrderDOList = new ArrayList<>();
        // 根据条件查询所有的订单
        Wrapper<BriFlightOrder> flightOrderWrapper = new EntityWrapper<>();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            flightOrderWrapper.eq(entry.getKey(), entry.getValue());
        }
        flightOrderWrapper.ne(true,"is_note",1);
        flightOrderWrapper.andNew(" id in ("+ids+")");
        List<BriFlightOrder> briFlightOrderList = briFlightOrderMapper.selectList(flightOrderWrapper);


        for (BriFlightOrder briFlightOrder : briFlightOrderList) {
            CnvFlightOrderDO cnvFlightOrderDO = new CnvFlightOrderDO();
            // 查询订单航班信息
            Wrapper<BriFlightInfo> flightInfoWrapper = new EntityWrapper<>();
            flightInfoWrapper.eq("order_no", briFlightOrder.getOrderNo());
            List<BriFlightInfo> briFlightInfoList = briFlightInfoService.selectList(flightInfoWrapper);
            if (briFlightInfoList.size() > 1) {
                BriFlightInfo briFlightInfoFirst;
                BriFlightInfo briFlightInfoSecond;
                //根据日期前后决定往返程
                if ((briFlightInfoList.get(1).getDepartureDate() != null && briFlightInfoList.get(1).getDepartureTime() != null)
                        && (briFlightInfoList.get(1).getDepartureDate().after(briFlightInfoList.get(0).getDepartureDate()))
                        || (briFlightInfoList.get(1).getDepartureDate().equals(briFlightInfoList.get(0).getDepartureDate())
                        && briFlightInfoList.get(1).getDepartureTime().after(briFlightInfoList.get(0).getDepartureTime()))) {


                    briFlightInfoFirst = briFlightInfoList.get(0);
                    briFlightInfoSecond = briFlightInfoList.get(1);
                } else {
                    briFlightInfoFirst = briFlightInfoList.get(1);
                    briFlightInfoSecond = briFlightInfoList.get(0);
                }
                //过滤未完成的订单
//                if (briFlightInfoSecond.getArriveDate() != null && (briFlightInfoSecond.getArriveDate().after(endDate) || briFlightInfoFirst.getArriveDate().before(beginDate))) {
//                    continue;
//                }
                /**
                 * 处理异常订单不考虑往返程问题
                 */

                cnvFlightOrderDO.setBoardPointName(briFlightInfoFirst.getBoardPointName());
                cnvFlightOrderDO.setBoardPointAirPortName(briFlightInfoFirst.getBoardPointAirPortName());
                cnvFlightOrderDO.setDepartureDate(briFlightInfoFirst.getDepartureDate());
                cnvFlightOrderDO.setDepartureTime(briFlightInfoFirst.getDepartureTime());
                cnvFlightOrderDO.setOffPointName(briFlightInfoFirst.getOffPointName());
                cnvFlightOrderDO.setOffPointAirPortName(briFlightInfoFirst.getOffPointAirPortName());
                cnvFlightOrderDO.setArriveDate(briFlightInfoFirst.getArriveDate());
                cnvFlightOrderDO.setArriveTime(briFlightInfoFirst.getArriveTime());
                cnvFlightOrderDO.setFlightNo(briFlightInfoFirst.getFlightNo());
                cnvFlightOrderDO.setYprice(briFlightInfoFirst.getYprice());
                cnvFlightOrderDO.setClassRebate(briFlightInfoFirst.getClassRebate());
                cnvFlightOrderDO.setClassLevelName(briFlightInfoFirst.getClassLevelName());
                cnvFlightOrderDO.setClassLevelCode(briFlightInfoFirst.getClassLevelCode());
                cnvFlightOrderDO.setLowestPrice(briFlightInfoFirst.getLowestPrice());

                cnvFlightOrderDO.setBoardPointName2(briFlightInfoSecond.getBoardPointName());
                cnvFlightOrderDO.setBoardPointAirPortName2(briFlightInfoSecond.getBoardPointAirPortName());
                cnvFlightOrderDO.setDepartureDate2(briFlightInfoSecond.getDepartureDate());
                cnvFlightOrderDO.setDepartureTime2(briFlightInfoSecond.getDepartureTime());
                cnvFlightOrderDO.setOffPointName2(briFlightInfoSecond.getOffPointName());
                cnvFlightOrderDO.setOffPointAirPortName2(briFlightInfoSecond.getOffPointAirPortName());
                cnvFlightOrderDO.setArriveDate2(briFlightInfoSecond.getArriveDate());
                cnvFlightOrderDO.setArriveTime2((briFlightInfoSecond.getArriveTime()));
                cnvFlightOrderDO.setFlightNo2(briFlightInfoSecond.getFlightNo());
                cnvFlightOrderDO.setYprice2(briFlightInfoSecond.getYprice());
                cnvFlightOrderDO.setClassRebate2(briFlightInfoSecond.getClassRebate());
                cnvFlightOrderDO.setClassLevelName2(briFlightInfoSecond.getClassLevelName());
                cnvFlightOrderDO.setClassLevelCode2(briFlightInfoSecond.getClassLevelCode());
                cnvFlightOrderDO.setLowestPrice2(briFlightInfoSecond.getLowestPrice());
            } else {
                BriFlightInfo briFlightInfoFirst = null;
                if (briFlightInfoFirst == null && briFlightInfoList.size() == 0){
                    continue;
                }
                briFlightInfoFirst = briFlightInfoList.get(0);
                //过滤未完成的订单
                /*
                处理异常订单不考虑往返程问题
                 */
//                if (briFlightInfoFirst.getArriveDate() != null && (briFlightInfoFirst.getArriveDate().after(endDate) || briFlightInfoFirst.getArriveDate().before(beginDate))) {
//                    continue;
//                }
                cnvFlightOrderDO.setBoardPointName(briFlightInfoFirst.getBoardPointName());
                cnvFlightOrderDO.setBoardPointAirPortName(briFlightInfoFirst.getBoardPointAirPortName());
                cnvFlightOrderDO.setDepartureDate(briFlightInfoFirst.getDepartureDate());
                cnvFlightOrderDO.setDepartureTime(briFlightInfoFirst.getDepartureTime());
                cnvFlightOrderDO.setOffPointName(briFlightInfoFirst.getOffPointName());
                cnvFlightOrderDO.setOffPointAirPortName(briFlightInfoFirst.getOffPointAirPortName());
                cnvFlightOrderDO.setArriveDate(briFlightInfoFirst.getArriveDate());
                cnvFlightOrderDO.setArriveTime(briFlightInfoFirst.getArriveTime());
                cnvFlightOrderDO.setFlightNo(briFlightInfoFirst.getFlightNo());
                cnvFlightOrderDO.setYprice(briFlightInfoFirst.getYprice());
                cnvFlightOrderDO.setClassRebate(briFlightInfoFirst.getClassRebate());
                cnvFlightOrderDO.setClassLevelName(briFlightInfoFirst.getClassLevelName());
                cnvFlightOrderDO.setClassLevelCode(briFlightInfoFirst.getClassLevelCode());
                cnvFlightOrderDO.setLowestPrice(briFlightInfoFirst.getLowestPrice());
            }

            // 机票订单（bri_flight_order）信息
            convertFromBriFlightOrder(briFlightOrder, cnvFlightOrderDO);
            //查询出行人
            Wrapper<BriFlightPassenger> flightPassengerWrapper = new EntityWrapper<>();
            flightPassengerWrapper.eq("order_no", briFlightOrder.getOrderNo());
            List<BriFlightPassenger> flightPassengerList = briFlightPassengerService.selectList(flightPassengerWrapper);
            convertFromBriFlightPassengers(cnvFlightOrderDO, flightPassengerList);
            cnvFlightOrderDOList.add(cnvFlightOrderDO);
        }
        return cnvFlightOrderDOList;
    }

    private void convertFromBriFlightPassengers(CnvFlightOrderDO cnvFlightOrderDO, List<BriFlightPassenger> flightPassengerList) {
        if (!CollectionUtils.isEmpty(flightPassengerList)) {
            StringBuffer passengerStringBuffer = new StringBuffer();
            for (int i = 0; i < flightPassengerList.size(); i++) {
                passengerStringBuffer.append(flightPassengerList.get(i).getPsgName());
                if (i != flightPassengerList.size() - 1) {
                    passengerStringBuffer.append(",");
                }
            }
            cnvFlightOrderDO.setPassenger(passengerStringBuffer.toString());
            cnvFlightOrderDO.setConsturctAndFuel(flightPassengerList.get(0).getConsturctAndFuel());
            cnvFlightOrderDO.setOAUserCode(flightPassengerList.get(0).getOAUserCode());
            cnvFlightOrderDO.setBindCounterFee(flightPassengerList.get(0).getBindCounterFee());
        }
    }

    private void convertFromBriFlightOrder(BriFlightOrder briFlightOrder, CnvFlightOrderDO cnvFlightOrderDO) {
        if (briFlightOrder != null) {
            cnvFlightOrderDO.setOrderNo(briFlightOrder.getOrderNo());
            cnvFlightOrderDO.setOrderType(briFlightOrder.getOrderType());
            cnvFlightOrderDO.setPublicOrPrivate(briFlightOrder.getPublicOrPrivate());
            cnvFlightOrderDO.setPreiumReason(briFlightOrder.getPreiumReason());
            cnvFlightOrderDO.setPreiumDetail(briFlightOrder.getPreiumDetail());
            cnvFlightOrderDO.setPaymentPrice(briFlightOrder.getPaymentPrice());
            cnvFlightOrderDO.setApplyOrderNo(briFlightOrder.getApplyOrderNo());
            cnvFlightOrderDO.setFarePrice(briFlightOrder.getFarePrice());
            cnvFlightOrderDO.setPsgCount(briFlightOrder.getPsgCount());
            cnvFlightOrderDO.setStatusType(briFlightOrder.getStatusType());
            cnvFlightOrderDO.setRefundPrice(briFlightOrder.getRefundPrice());
            cnvFlightOrderDO.setTpPrice(briFlightOrder.getTpPrice());
            cnvFlightOrderDO.setTripType(briFlightOrder.getTripType());
            cnvFlightOrderDO.setRelatedNo(briFlightOrder.getRelatedNo());
            cnvFlightOrderDO.setRelatedType(briFlightOrder.getRelatedType());
            cnvFlightOrderDO.setCreateBy(briFlightOrder.getCreateBy());
            cnvFlightOrderDO.setOrderTime(briFlightOrder.getOrderTime());
            cnvFlightOrderDO.setFromType(briFlightOrder.getFromType());
            cnvFlightOrderDO.setNoOrderLowstReason(briFlightOrder.getNoOrderLowstReason());
            cnvFlightOrderDO.setNote(briFlightOrder.getNote());
            cnvFlightOrderDO.setTenant(briFlightOrder.getTenant());
            cnvFlightOrderDO.setUserId(briFlightOrder.getUserId());
            cnvFlightOrderDO.setPayType(briFlightOrder.getPayType());
            cnvFlightOrderDO.setPaymentType(briFlightOrder.getPaymentType());
            cnvFlightOrderDO.setCategory(briFlightOrder.getCategory());
            cnvFlightOrderDO.setId(briFlightOrder.getId());
            cnvFlightOrderDO.setParentOrderNo(briFlightOrder.getParentOrderNo());
            cnvFlightOrderDO.setChangeFee(briFlightOrder.getChangeFee());
            cnvFlightOrderDO.setSendTicketFee(briFlightOrder.getSendTicketFee());
            cnvFlightOrderDO.setSumCounterFee(briFlightOrder.getSumCounterFee());
            cnvFlightOrderDO.setSumInsMoney(briFlightOrder.getSumInsMoney());
        }
    }

    /*
    end
     */
    public List<BriFlightOrder> getOrdersByRelateYBZId(List<String> ybzIds)
    {
        if(ybzIds!=null&&ybzIds.size()>0) {
            return briFlightOrderMapper.getFlightOrdersByRelateYBZIds(ybzIds);
        }
        else
        {
            return  new ArrayList<BriFlightOrder>();
        }
    }

    @Override
    public void updateSQByRelateYbzId(JSONArray relateYBZIds, String tenantId) {
        if (!CollectionUtils.isEmpty(relateYBZIds)) {
            for (Object relateYBZIdObject : relateYBZIds) {
                String relateYBZId = String.valueOf(relateYBZIdObject);
                EntityWrapper<BriFlightOrder> flightOrderEntityWrapper = new EntityWrapper<>();
                flightOrderEntityWrapper.where("tenant={0}", tenantId);
                flightOrderEntityWrapper.and("relateYbzId={0}", relateYBZId);
                List<BriFlightOrder> briFlightOrders = this.selectList(flightOrderEntityWrapper);

            }
        }
    }

    @Override
    public List<BriFlightOrder> getFlightOrdersByTimeRange(String tenantId, Date begin, Date end, String isDomc) {
        return briFlightOrderMapper.getFlightOrdersByTimeRange(tenantId, begin, end, isDomc);
    }

    /**
     * 查询机票订单详情
     * @param params
     * @return
     */
    @Override
    public JSONObject getFlightOrderJson(JSONObject params) {
        return null;
    }


    @Override
    public BriFlightOrder getFlightOrderJson(String tenant, String orderNo) {
        Wrapper<BriFlightOrder> wrapper = new EntityWrapper<>();
        wrapper.eq("tenant", tenant);
        wrapper.eq("order_no", orderNo);
        List<BriFlightOrder> briFlightOrders = briFlightOrderMapper.selectList(wrapper);
        if (briFlightOrders.size() > 0){
            return briFlightOrders.get(0);
        }
        return null;
    }

    @Override
    public CnvFlightOrderDO getBriFlightOrderByOrderNo(String orderNo) {
        CnvFlightOrderDO cnvFlightOrderDO = new CnvFlightOrderDO();
        BriFlightOrder briFlightOrder = briFlightOrderMapper.getBriFlightOrderByOrderNo(orderNo);
        List<BriFlightInfo> briFlightInfos = briFlightInfoService.getBriFlightInfo(orderNo);
        List<BriFlightPassenger> briFlightPassengers = briFlightPassengerService.getBriFlightPassenger(orderNo);
        convertToCnvFlightOrderDO(cnvFlightOrderDO, briFlightOrder, briFlightInfos, briFlightPassengers);
        return cnvFlightOrderDO;
    }

    /**
     * 国贸私有化
     * 通过友报账向NCC发送请求
     *  add by yangxw1
     *  2021年6月29日
     * @param url
     * @param paeamOBJ
     * @return
     */
    @Override
    public String  punctureNCC(String url ,JSONObject paeamOBJ){
        JSONObject paeam = new JSONObject();
        paeam.put("paeamOBJ",paeamOBJ);
        paeam.put("urlKey",url);
        //POST=到NCC进行穿透
        String ybzurl = ybzBaseUrl+"/open/brigade/nodeRef/";
        logger.error("友报账进行地址穿透--url:"+ybzurl+" paeam:"+paeam.toString());
        String result = sendPost(ybzurl, paeam.toString());
        logger.error("NCC返回数据result"+result);
        return result;
    }

    @Override
    public List<CnvFlightOrderDO> getBriFlightOrderLikeOrderNo(String orderNo, String fromType, String tenant){
        List<CnvFlightOrderDO> list = new ArrayList<>();
        if ("clyh".equals(fromType)){
            Wrapper<BriFlightOrder> wrapper = new EntityWrapper<>();
            wrapper.eq(BriFlightOrder.TENANT, tenant);
            wrapper.like("order_no", orderNo, SqlLike.RIGHT);
            List<BriFlightOrder> briFlightOrders = this.selectList(wrapper);
            for (BriFlightOrder briFlightOrder : briFlightOrders) {

                CnvFlightOrderDO cnvFlightOrderDO = new CnvFlightOrderDO();
                String briFlightOrderNo = briFlightOrder.getOrderNo();
                List<BriFlightInfo> briFlightInfos = briFlightInfoService.getBriFlightInfo(briFlightOrderNo);
                List<BriFlightPassenger> briFlightPassengers = briFlightPassengerService.getBriFlightPassenger(briFlightOrderNo);
                if (briFlightOrder != null && briFlightInfos != null && briFlightPassengers != null){
                    convertToCnvFlightOrderDO(cnvFlightOrderDO, briFlightOrder, briFlightInfos, briFlightPassengers);
                }
                list.add(cnvFlightOrderDO);
            }
        }else {
            CnvFlightOrderDO cnvFlightOrderDO = getBriFlightOrderByOrderNo(orderNo);
            list.add(cnvFlightOrderDO);
        }

        return  list;
    }
    private List<BriFlightOrder> getBriFlightOrderLikeOrderNoV2(String orderNo, String fromType, String tenant){
        List<BriFlightOrder> list = new ArrayList<>();
        if ("clyh".equals(fromType)){
            Wrapper<BriFlightOrder> wrapper = new EntityWrapper<>();
            wrapper.eq(BriFlightOrder.TENANT, tenant);
            wrapper.like("order_no", orderNo, SqlLike.RIGHT);
            List<BriFlightOrder> briFlightOrders = this.selectList(wrapper);
            if (!CollectionUtils.isEmpty(briFlightOrders)) {
                for (BriFlightOrder briFlightOrder : briFlightOrders) {
                    String briFlightOrderNo = briFlightOrder.getOrderNo();
                    List<BriFlightInfo> briFlightInfos = briFlightInfoService.getBriFlightInfo(briFlightOrderNo);
                    List<BriFlightPassenger> briFlightPassengers = briFlightPassengerService.getBriFlightPassenger(briFlightOrderNo);
                    if (briFlightOrder != null && briFlightInfos != null && briFlightPassengers != null){
                        list.add(briFlightOrder);
                    }
                }
            }
        } else {
            BriFlightOrder flightOrder = briFlightOrderMapper.getBriFlightOrderByOrderNo(orderNo);
            if (flightOrder != null) {
                list.add(flightOrder);
            }
        }
        return  list;
    }

    @Override
    public List<CnvFlightOrderDO> getRelationOrders(String orderNo, String parentOrderNo, String fromType, String tenant) {
        List<CnvFlightOrderDO> orderDOList = new ArrayList<>();
        boolean isClyh = "clyh".equals(fromType);
        boolean hasPsgId = orderNo.split("_").length > 1;
        String passengerId = isClyh && hasPsgId ? orderNo.split("_")[1] : "";
        //查询是否有下游单据
        List<BriFlightOrder> briFlightOrders = queryLowerOrders(orderNo, isClyh, tenant, fromType);
        for (BriFlightOrder briFlightOrder : briFlightOrders) {
            String tgOrderNo = briFlightOrder.getOrderNo();
            // 其退改单的出行人与其订单的出行人身份证一致
            if (isClyh && hasPsgId  && !passengerId.equals(tgOrderNo.split("_")[1])){
                continue;
            }
            fillAndConvert(briFlightOrder, orderDOList);

            //这个单据可能是改签单，查询是否有下游单据
            List<BriFlightOrder> tgOrderList = queryLowerOrders(tgOrderNo, isClyh, tenant, fromType);
            for (BriFlightOrder tgOrder : tgOrderList) {
                String gOrderNo = tgOrder.getOrderNo();
                // 其退改单的出行人与其订单的出行人身份证一致
                if (isClyh && hasPsgId  && !passengerId.equals(gOrderNo.split("_")[1])){
                    continue;
                }
                fillAndConvert(tgOrder, orderDOList);

                //这个单据仍然可能是改签单，查询是否有下游单据, 是否有退单
                List<BriFlightOrder> tOrderList = queryLowerOrders(gOrderNo, isClyh, tenant, fromType);
                for (BriFlightOrder tOrder : tOrderList) {

                    String tOrderNo = tOrder.getOrderNo();
                    // 其退改单的出行人与其订单的出行人身份证一致
                    if (isClyh && hasPsgId  && !passengerId.equals(tOrderNo.split("_")[1])){
                        continue;
                    }
                    fillAndConvert(tOrder, orderDOList);
                }

            }

        }

        //查看该订单是否有原单
        if (StringUtil.isNotEmpty(parentOrderNo) && (!orderNo.equals(parentOrderNo) && !orderNo.split("_")[0].equals(parentOrderNo))){
            //查询是否有上游单据
            List<CnvFlightOrderDO> briFlightOrde = this.getBriFlightOrderLikeOrderNo(parentOrderNo, fromType, tenant);
            for (CnvFlightOrderDO cnvFlightOrderDO : briFlightOrde) {
                String cnvOderNo = cnvFlightOrderDO.getOrderNo();
                if (StringUtil.isEmpty(cnvOderNo) || (isClyh && hasPsgId  && !passengerId.equals(cnvOderNo.split("_")[1])) ){
                    continue;
                }
                orderDOList.add(cnvFlightOrderDO);

                //该原单可能是改签单，查询是否还有上游单据
                String gParentOrderNO = cnvFlightOrderDO.getParentOrderNo();
                if (StringUtil.isNotEmpty(gParentOrderNO) && !cnvOderNo.equals(gParentOrderNO) && !cnvOderNo.split("_").equals(gParentOrderNO)){
                    List<CnvFlightOrderDO> ogBriFlightOrde = this.getBriFlightOrderLikeOrderNo(gParentOrderNO, fromType,tenant);
                    for (CnvFlightOrderDO ogOrder : ogBriFlightOrde) {
                        String ogOrderNo = ogOrder.getOrderNo();
                        if (StringUtil.isEmpty(ogOrderNo) || (isClyh && hasPsgId  && !passengerId.equals(ogOrderNo.split("_")[1])) ){
                            continue;
                        }
                        orderDOList.add(ogOrder);

                        //该原单仍然可能是改签单，查询是否还有上游单据,查询初始单据
                        String ogParentOrderNO = ogOrder.getParentOrderNo();
                        if (StringUtil.isNotEmpty(ogParentOrderNO) && !ogOrderNo.equals(ogParentOrderNO)) {
                            List<CnvFlightOrderDO> oBriFlightOrde = this.getBriFlightOrderLikeOrderNo(ogParentOrderNO, fromType, tenant);
                            for (CnvFlightOrderDO oOrderDO : oBriFlightOrde) {
                                String oOderNo = oOrderDO.getOrderNo();
                                if (StringUtil.isEmpty(oOderNo) || (isClyh && hasPsgId  && !passengerId.equals(ogOrderNo.split("_")[1])) ){
                                    continue;
                                }
                                orderDOList.add(oOrderDO);
                            }
                        }
                    }
                }else {
                    // 关联关系可能：退改单与原单关联不是顺序的，退改单的原单号都是原单
                    List<BriFlightOrder> sameLevelOrderList = queryLowerOrders(cnvOderNo, isClyh, tenant, fromType);
                    if (sameLevelOrderList != null){
                        for (BriFlightOrder sLOrder : sameLevelOrderList) {
                            String slOrderNo = sLOrder.getOrderNo();
                            if (StringUtil.isEmpty(slOrderNo) || (isClyh && hasPsgId  && !passengerId.equals(slOrderNo.split("_")[1])) || slOrderNo.equals(orderNo)){
                                continue;
                            }
                            fillAndConvert(sLOrder, orderDOList);

                        }
                    }
                }


            }

        }
        orderDOList = distinct(orderDOList,  orderNo);
        return orderDOList;
    }

    @Override
    public List<BriFlightOrder> getRelationOrdersV2(String orderNo, String parentOrderNo, String fromType, String tenant) {
        List<BriFlightOrder> resultOrderList = new ArrayList<>();
        boolean isClyh = "clyh".equals(fromType);
        boolean hasPsgId = orderNo.split("_").length > 1;
        String passengerId = isClyh && hasPsgId ? orderNo.split("_")[1] : "";
        //查询是否有下游单据
        List<BriFlightOrder> briFlightOrders = queryLowerOrders(orderNo, isClyh, tenant, fromType);
        for (BriFlightOrder briFlightOrder : briFlightOrders) {
            String tgOrderNo = briFlightOrder.getOrderNo();
            // 其退改单的出行人与其订单的出行人身份证一致
            if (isClyh && hasPsgId  && !passengerId.equals(tgOrderNo.split("_")[1])){
                continue;
            }
            resultOrderList.add(briFlightOrder);
            //这个单据可能是改签单，查询是否有下游单据
            List<BriFlightOrder> tgOrderList = queryLowerOrders(tgOrderNo, isClyh, tenant, fromType);
            for (BriFlightOrder tgOrder : tgOrderList) {
                String gOrderNo = tgOrder.getOrderNo();
                // 其退改单的出行人与其订单的出行人身份证一致
                if (isClyh && hasPsgId  && !passengerId.equals(gOrderNo.split("_")[1])){
                    continue;
                }
                resultOrderList.add(tgOrder);
                //这个单据仍然可能是改签单，查询是否有下游单据, 是否有退单
                List<BriFlightOrder> tOrderList = queryLowerOrders(gOrderNo, isClyh, tenant, fromType);
                for (BriFlightOrder tOrder : tOrderList) {
                    String tOrderNo = tOrder.getOrderNo();
                    // 其退改单的出行人与其订单的出行人身份证一致
                    if (isClyh && hasPsgId  && !passengerId.equals(tOrderNo.split("_")[1])){
                        continue;
                    }
                    resultOrderList.add(tOrder);
                }

            }

        }

        //查看该订单是否有原单
        if (StringUtil.isNotEmpty(parentOrderNo) && (!orderNo.equals(parentOrderNo) && !orderNo.split("_")[0].equals(parentOrderNo))){
            //查询是否有上游单据
            List<BriFlightOrder> briFlightOrderList = this.getBriFlightOrderLikeOrderNoV2(parentOrderNo, fromType, tenant);
            for (BriFlightOrder cnvFlightOrderDO : briFlightOrderList) {
                String cnvOderNo = cnvFlightOrderDO.getOrderNo();
                if (StringUtil.isEmpty(cnvOderNo) || (isClyh && hasPsgId  && !passengerId.equals(cnvOderNo.split("_")[1])) ){
                    continue;
                }
                resultOrderList.add(cnvFlightOrderDO);

                //该原单可能是改签单，查询是否还有上游单据
                String gParentOrderNO = cnvFlightOrderDO.getParentOrderNo();
                if (StringUtil.isNotEmpty(gParentOrderNO) && !cnvOderNo.equals(gParentOrderNO) && !cnvOderNo.split("_")[0].equals(gParentOrderNO)){
                    List<BriFlightOrder> ogBriFlightOrde = this.getBriFlightOrderLikeOrderNoV2(gParentOrderNO, fromType, tenant);
                    for (BriFlightOrder ogOrder : ogBriFlightOrde) {
                        String ogOrderNo = ogOrder.getOrderNo();
                        if (StringUtil.isEmpty(ogOrderNo) || (isClyh && hasPsgId  && !passengerId.equals(ogOrderNo.split("_")[1])) ){
                            continue;
                        }
                        resultOrderList.add(ogOrder);

                        //该原单仍然可能是改签单，查询是否还有上游单据,查询初始单据
                        String ogParentOrderNO = ogOrder.getParentOrderNo();
                        if (StringUtil.isNotEmpty(ogParentOrderNO) && !ogOrderNo.equals(ogParentOrderNO)) {
                            List<BriFlightOrder> oBriFlightOrde = this.getBriFlightOrderLikeOrderNoV2(ogParentOrderNO, fromType, tenant);
                            for (BriFlightOrder oOrderDO : oBriFlightOrde) {
                                String oOderNo = oOrderDO.getOrderNo();
                                if (StringUtil.isEmpty(oOderNo) || (isClyh && hasPsgId  && !passengerId.equals(ogOrderNo.split("_")[1])) ){
                                    continue;
                                }
                                resultOrderList.add(oOrderDO);
                            }
                        }
                    }
                }else {
                    // 关联关系可能：退改单与原单关联不是顺序的，退改单的原单号都是原单
                    List<BriFlightOrder> sameLevelOrderList = queryLowerOrders(cnvOderNo, isClyh, tenant, fromType);
                    if (sameLevelOrderList != null){
                        for (BriFlightOrder sLOrder : sameLevelOrderList) {
                            String slOrderNo = sLOrder.getOrderNo();
                            if (StringUtil.isEmpty(slOrderNo) || (isClyh && hasPsgId  && !passengerId.equals(slOrderNo.split("_")[1])) || slOrderNo.equals(orderNo)){
                                continue;
                            }
                            resultOrderList.add(sLOrder);
                        }
                    }
                }


            }

        }
        resultOrderList = distinctV2(resultOrderList, orderNo);
        return resultOrderList;
    }


    /**
     * 查询下游单据，查看该订单是否是其他订单的原单
     *
     * @param orderNo
     * @param isClyh  是否是差旅壹号的订单
     * @return
     */
    private List<BriFlightOrder> queryLowerOrders(String orderNo, boolean isClyh, String tenant, String fromType) {
        String[] orderSplilt = orderNo.split("_");
        //查看该订单是否是其他订单的原单
        Wrapper<BriFlightOrder> wrapper = new EntityWrapper<>();
        wrapper.eq(BriFlightOrder.TENANT, tenant);
        if (isClyh) {
            wrapper.like("parent_order_no", orderSplilt[0], SqlLike.RIGHT);
        } else {
            wrapper.eq("parent_order_no", orderNo);
        }
        List<BriFlightOrder> briFlightOrders = this.selectList(wrapper);
        //携程大客户退改签 和 出票 orderNo相同，没有用parent_order_no关联
        if (ChannelEnum.CTRIPBC.getName().equalsIgnoreCase(fromType)) {
            wrapper = new EntityWrapper<>();
            wrapper.eq(BriFlightOrder.TENANT, tenant);
            wrapper.like("order_no", orderSplilt[0], SqlLike.RIGHT);
            briFlightOrders = this.selectList(wrapper);
        }
        List<BriFlightOrder> rsList = new ArrayList<>();
        for (BriFlightOrder briFlightOrder : briFlightOrders) {
            if (orderNo.equals(briFlightOrder.getOrderNo())) {
                continue;
            }
            rsList.add(briFlightOrder);
        }
        return rsList;
    }

    private void fillAndConvert(BriFlightOrder briFlightOrder, List<CnvFlightOrderDO> orderDOList){
        CnvFlightOrderDO cnvFlightOrderDO = new CnvFlightOrderDO();
        List<BriFlightInfo> briFlightInfos = briFlightInfoService.getBriFlightInfo(briFlightOrder.getOrderNo());
        List<BriFlightPassenger> briFlightPassengers = briFlightPassengerService.getBriFlightPassenger(briFlightOrder.getOrderNo());
        convertToCnvFlightOrderDO(cnvFlightOrderDO, briFlightOrder, briFlightInfos, briFlightPassengers);
        orderDOList.add(cnvFlightOrderDO);
    }

    @Override
    public JSONArray getFlightOrders(String userId, String bxState, String beginDate, String endDate, String tenantId, Map<String, List<Map<String, String>>> canBeSeen) {
        List<BriFlightOrder> briFlightOrders = this.getFlightOrdersByBxState(userId, bxState, beginDate, endDate, tenantId, canBeSeen);
        return getOrdersDetails(briFlightOrders, true);
    }

    @Override
    public JSONArray getFlightOrders(String tenant, String orderNo) {
        JSONArray result = new JSONArray();
        Wrapper<BriFlightOrder> wrapper = new EntityWrapper<>();
        wrapper.eq(BriFlightOrder.TENANT, tenant);
        wrapper.eq(BriFlightOrder.ORDER_NO, orderNo);
        BriFlightOrder briFlightOrder = this.selectOne(wrapper);
        List<BriFlightOrder> orderList = new ArrayList<>();
        if (briFlightOrder != null){
            orderList.add(briFlightOrder);
            List<BriFlightOrder> relationOrderList = getRelationOrdersV2(briFlightOrder.getOrderNo(), briFlightOrder.getParentOrderNo(), briFlightOrder.getFromType(), tenant);
            orderList.addAll(relationOrderList);
            result.addAll(getOrdersDetails(orderList, false));
        }
        return result;
    }

    private JSONArray getOrdersDetails(List<BriFlightOrder> briFlightOrders, Boolean isFilter){
        JSONArray flightOrderArr = new JSONArray();
        Iterator<BriFlightOrder> iterator = briFlightOrders.iterator();
        while (iterator.hasNext()){
            BriFlightOrder briFlightOrder = iterator.next();
            String orderNo = briFlightOrder.getOrderNo();
            JSONObject orderJSON = objToJSONObject(briFlightOrder);
            //查询机票航程信息
            List<BriFlightInfo> infoList = briFlightInfoService.getBriFlightInfo(orderNo);
            if (infoList != null){
                boolean hasTraveled = false;
                for (BriFlightInfo briFlightInfo : infoList) {
                    if (briFlightInfo.getArriveDate() == null || new Date().before(briFlightInfo.getArriveDate())) {
                        // 未出行
                        hasTraveled = true;
                        break;
                    }
                }
                if (isFilter && hasTraveled){
                    // 未出行
                    iterator.remove();
                    continue;
                }
            }

            JSONArray infoArr = listToJSONArray(infoList);
            orderJSON.put("flightInfos", infoArr);
            //飞机乘机人信息
            List<BriFlightPassenger> passengerList = briFlightPassengerService.getBriFlightPassenger(orderNo);
            JSONArray passengerArr = listToJSONArray(passengerList);
            orderJSON.put("flightPassengers", passengerArr);
            //飞机联系人信息
            List<BriFlightOrderContact> contactList = flightContactService.getBriFlightOrderContact(orderNo);
            JSONArray contactArr = listToJSONArray(contactList);
            orderJSON.put("flightOrderContacts", contactArr);
            //发票信息
            List<InvoiceRelationship> relationshipList = relationshipService.getRelationshipsByOrderNo(orderNo);
            JSONArray relationArr = listToJSONArray(relationshipList);
            orderJSON.put("invoices", relationArr);
            flightOrderArr.add(orderJSON);
        }

        return flightOrderArr;
    }

    @Override
    public List<BriFlightOrder> getFlightOrdersByBxState(String userId, String bxState, String beginDate, String endDate, String tenantId, Map<String, List<Map<String, String>>> canBeSeen){
        return briFlightOrderMapper.getOrdersByBxState(userId, bxState, beginDate, endDate, tenantId, canBeSeen);
    }

    @Override
    public BriFlightOrder getFlightOrderByOrderNo(String orderNo) {
        return briFlightOrderMapper.getBriFlightOrderByOrderNo(orderNo);
    }

    @Override
    public List<BriFlightOrder> getBriFlightOrderByParentOrderNo(String orderNo) {
        return briFlightOrderMapper.getBriFlightOrderByParentOrderNo(orderNo);
    }

    /**
     * 将对象转成JSONObject
     * @param object
     * @return
     */
    private JSONObject objToJSONObject(Object object){
        String jsonString = JSONObject.toJSONString(object);
        return JSONObject.parseObject(jsonString);
    }

    /**
     * 将list转成JSONArray
     * @param list
     * @return
     */
    private JSONArray listToJSONArray(List list){
        String jsonString = JSONArray.toJSONString(list);
        return JSONArray.parseArray(jsonString);
    }

    private void convertToCnvFlightOrderDO(CnvFlightOrderDO cnvFlightOrderDO, BriFlightOrder briFlightOrder, List<BriFlightInfo> briFlightInfos, List<BriFlightPassenger> briFlightPassengers) {
        convertFromBriFlightOrder(briFlightOrder, cnvFlightOrderDO);
        convertFromBriFlightInfos(cnvFlightOrderDO, briFlightInfos);
        convertFromBriFlightPassengers(cnvFlightOrderDO, briFlightPassengers);
    }

    private void convertFromBriFlightInfos(CnvFlightOrderDO cnvFlightOrderDO, List<BriFlightInfo> briFlightInfos) {
        if (!CollectionUtils.isEmpty(briFlightInfos)) {
            if (briFlightInfos.size() == 1) {
                BriFlightInfo briFlightInfo = briFlightInfos.get(0);
                cnvFlightOrderDO.setBoardPointName(briFlightInfo.getBoardPointName());
                cnvFlightOrderDO.setBoardPointAirPortName(briFlightInfo.getBoardPointAirPortName());
                cnvFlightOrderDO.setDepartureDate(briFlightInfo.getDepartureDate());
                cnvFlightOrderDO.setDepartureTime(briFlightInfo.getDepartureTime());
                cnvFlightOrderDO.setOffPointName(briFlightInfo.getOffPointName());
                cnvFlightOrderDO.setOffPointAirPortName(briFlightInfo.getOffPointAirPortName());
                cnvFlightOrderDO.setArriveDate(briFlightInfo.getArriveDate());
                cnvFlightOrderDO.setArriveTime(briFlightInfo.getArriveTime());
                cnvFlightOrderDO.setFlightNo(briFlightInfo.getFlightNo());
                cnvFlightOrderDO.setYprice(briFlightInfo.getYprice());
                cnvFlightOrderDO.setClassRebate(briFlightInfo.getClassRebate());
                cnvFlightOrderDO.setClassLevelName(briFlightInfo.getClassLevelName());
                cnvFlightOrderDO.setClassLevelCode(briFlightInfo.getClassLevelCode());
                cnvFlightOrderDO.setLowestPrice(briFlightInfo.getLowestPrice());
                cnvFlightOrderDO.setOilfee(briFlightInfo.getOilfee());
                cnvFlightOrderDO.setConstructionPrice(briFlightInfo.getConstructionPrice());
            } else if (briFlightInfos.size() > 1) {
                Collections.sort(briFlightInfos);
                BriFlightInfo briFlightInfoFirst = briFlightInfos.get(0);
                BriFlightInfo briFlightInfoLast = briFlightInfos.get(briFlightInfos.size() - 1);
                cnvFlightOrderDO.setBoardPointName(briFlightInfoFirst.getBoardPointName());
                cnvFlightOrderDO.setBoardPointAirPortName(briFlightInfoFirst.getBoardPointAirPortName());
                cnvFlightOrderDO.setDepartureDate(briFlightInfoFirst.getDepartureDate());
                cnvFlightOrderDO.setDepartureTime(briFlightInfoFirst.getDepartureTime());
                cnvFlightOrderDO.setOffPointName(briFlightInfoLast.getOffPointName());
                cnvFlightOrderDO.setOffPointAirPortName(briFlightInfoLast.getOffPointAirPortName());
                cnvFlightOrderDO.setArriveDate(briFlightInfoLast.getArriveDate());
                cnvFlightOrderDO.setArriveTime(briFlightInfoLast.getArriveTime());
                cnvFlightOrderDO.setFlightNo(briFlightInfoFirst.getFlightNo());
                cnvFlightOrderDO.setYprice(briFlightInfoFirst.getYprice());
                cnvFlightOrderDO.setClassRebate(briFlightInfoFirst.getClassRebate());
                cnvFlightOrderDO.setClassLevelName(briFlightInfoFirst.getClassLevelName());
                cnvFlightOrderDO.setClassLevelCode(briFlightInfoFirst.getClassLevelCode());
                cnvFlightOrderDO.setLowestPrice(briFlightInfoFirst.getLowestPrice());
                BigDecimal oilFee = new BigDecimal(0);
                BigDecimal constructionPrice = BigDecimal.ZERO;
                for (BriFlightInfo briFlightInfo : briFlightInfos) {
                    if (briFlightInfo.getOilfee() != null){
                        oilFee = oilFee.add(briFlightInfo.getOilfee());
                    }
                    if (briFlightInfo.getConstructionPrice() != null) {
                        constructionPrice = constructionPrice.add(briFlightInfo.getConstructionPrice());
                    }
                }
                cnvFlightOrderDO.setConstructionPrice(constructionPrice);
                cnvFlightOrderDO.setOilfee(oilFee);
            }
        }
    }

    private static List<CnvFlightOrderDO> distinct(List<CnvFlightOrderDO> cnvFlightOrderDOList, String orderNo) {
        HashMap<String, CnvFlightOrderDO> map = new HashMap<>();
        for (CnvFlightOrderDO cnvFlightOrderDO : cnvFlightOrderDOList) {
            map.put(cnvFlightOrderDO.getOrderNo(), cnvFlightOrderDO);
        }
        cnvFlightOrderDOList = new ArrayList<CnvFlightOrderDO>();
        for (Map.Entry<String, CnvFlightOrderDO> entry : map.entrySet()) {
            if (!orderNo.equals(entry.getKey())){
                cnvFlightOrderDOList.add(entry.getValue());
            }
        }
        return cnvFlightOrderDOList;
    }
    private static List<BriFlightOrder> distinctV2(List<BriFlightOrder> cnvFlightOrderDOList, String orderNo) {
        HashMap<String, BriFlightOrder> map = new HashMap<>();
        for (BriFlightOrder cnvFlightOrderDO : cnvFlightOrderDOList) {
            map.put(cnvFlightOrderDO.getOrderNo(), cnvFlightOrderDO);
        }
        cnvFlightOrderDOList = new ArrayList<BriFlightOrder>();
        for (Map.Entry<String, BriFlightOrder> entry : map.entrySet()) {
            if (!orderNo.equals(entry.getKey())){
                cnvFlightOrderDOList.add(entry.getValue());
            }
        }
        return cnvFlightOrderDOList;
    }
}
